package br.com.erpnasajon.utils;

import br.com.erpnasajon.modelo.Acesso;
import br.com.erpnasajon.modelo.Cliente;
import br.com.erpnasajon.modelo.PerfilUsuario;
import br.com.erpnasajon.modelo.Usuario;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author MENDONCA
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    public HibernateUtil() {
    }

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(Cliente.class);
                ac.addAnnotatedClass(PerfilUsuario.class);
                ac.addAnnotatedClass(Usuario.class);
                ac.addAnnotatedClass(Acesso.class);
                sessionFactory = ac.configure().buildSessionFactory();
            } catch (Throwable ex) {
                System.err.println("Erro na inicialização do Session Factory [" + ex.getMessage() + "]");
                throw new ExceptionInInitializerError(ex);
            }
        }
        return sessionFactory;
    }
}
